package com.fili.gui.dialogs;

import com.fili.core.keygen.EncodedPermission;
import com.fili.core.params.DryParams;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 14, 2006
 * Time: 11:37:36 PM
 */
public class RegisterMessageDialog extends JDialog implements ActionListener
{
    private JButton btnStart;
    private JButton btnQuit;
    private JButton btnRegister;

    private boolean modalResult;
    private EncodedPermission encodedPermission;


    public EncodedPermission getEncodedPermission()
    {
        return encodedPermission;
    }

    public RegisterMessageDialog(Frame owner, String message, boolean canRun)
    {
        super(owner, "Va rugam inregistrati", true);

        setLayout(null);
        String[] lines = message.split("\\n");
        int y = 10;
        for (String line : lines)
        {
            JLabel lbl = new JLabel(line);
            lbl.setOpaque(true);
            lbl.setBackground(Color.yellow);
            lbl.setBounds(10, y, 270, 15);
            add(lbl);
            y += 15;
        }

        y += 15;

        btnStart = new JButton("Start");
        btnStart.setBounds(10, y, 60, 25);
        btnStart.addActionListener(this);
        btnStart.setEnabled(canRun);
        add(btnStart);

        btnQuit = new JButton("Quit");
        btnQuit.setBounds(75, y, 60, 25);
        btnQuit.addActionListener(this);
        add(btnQuit);

        btnRegister = new JButton("Register...");
        btnRegister.setBounds(190, y, 90, 25);
        btnRegister.setFont(btnRegister.getFont().deriveFont(Font.BOLD));
        btnRegister.setMargin(new Insets(0,0,0,0));
        btnRegister.addActionListener(this);
        add(btnRegister);

        y += 30;
        setSize(300, y + 38);
        encodedPermission = null;
        setLocationRelativeTo(null);
    }

    public boolean showModal()
    {
        modalResult = false;
        setVisible(true);
        return modalResult;
    }

    public void actionPerformed(ActionEvent e)
    {
        if (btnQuit.equals(e.getSource()))
        {
            modalResult = false;
            setVisible(false);
            DryParams.saveParams();
        }

        if (btnStart.equals(e.getSource()))
        {
            modalResult = true;
            setVisible(false);
            DryParams.saveParams();
        }

        if (btnRegister.equals(e.getSource()))
        {
            RegisterDialog rd = new RegisterDialog(this);
            encodedPermission = rd.showModal();
            if (encodedPermission != null)
            {
                modalResult = true;
                setVisible(false);
                DryParams.saveParams();                
            }
        }
    }
}
